/**
 * 
 */
package com.lvan.controller.system;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lvan.bean.Page;
import com.lvan.bean.Result;
import com.lvan.bean.User;
import com.lvan.bean.BaseException;
import com.lvan.constant.Unlogin;
import com.lvan.controller.BaseController;
import com.lvan.service.interfaze.system.IUserService;
import com.lvan.util.MD5Util;

/**
 * @author Stern He<br>
 *         2013-9-12<br>
 * @version 1.0.0.0
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    private IUserService userService;

    @Unlogin
    @RequestMapping(value = "/login")
    public String login(ModelMap model,User user,HttpServletRequest request) throws BaseException {
        User u = userService.login(user,getIp(request));
        Result result = new Result();
        result.setData(u);
        model.put("data", result);
        request.getSession().setAttribute("user", u);
        return INDEX;
    }
    
    @Unlogin
    @RequestMapping(value = "/add",method=RequestMethod.POST)
    public String add(ModelMap model, User user,HttpServletRequest request) throws BaseException {
    	userService.add(user);
    	model.put("data", true);
    	return INDEX;
    }
    
    @RequestMapping(value="/delete")
    public String delete(User user,HttpServletRequest request) throws BaseException{
    	if(user.getIds()!=null)
    		userService.delete(user.getIds());
    	else
    		userService.delete(user.getUserId());
    	return INDEX;
    }
    @RequestMapping(value="/update")
    public String update(User user,String oldPwd,HttpServletRequest request) throws BaseException{
    	User curUser = (User) request.getSession().getAttribute("user");
    	if(!curUser.getUserPwd().equals(MD5Util.MD5Encode(oldPwd))) {
    		throw new BaseException("系统提示",IUserService.PWD_ERROR, "密码错误");
    	}
    	user.setUserId(curUser.getUserId());
    	userService.update(user);
    	return INDEX;
    }

    public IUserService getUserService() {
        return userService;
    }
    
    @RequestMapping(value="/query")
    public String query(ModelMap model,Page page,User user,HttpServletRequest request) throws BaseException{
    	List<User> us = userService.query(user, page);
    	model.put("data", us);
    	return INDEX;
    }
    
    @Unlogin
    @RequestMapping(value="/exist")
    public Object existUserByName(ModelMap model,String name) throws BaseException {
        User u = new User();
        u.setUserName(name);
        model.put("data", this.userService.hasUseByName(u));
        return DEFAULT;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
    
    @RequestMapping(value="/logout")
    public void logout(HttpServletRequest request,HttpServletResponse response) throws BaseException, ServletException, IOException{
    	User user = (User) request.getSession().getAttribute("user");
    	if(user!=null) {
    		this.userService.logout(user);
    		request.getSession().setAttribute("user", null);
    	}
    	request.getRequestDispatcher("/index.html").forward(request, response);
    }
}
